package com.tongshu.miaosha.controller;

import com.tongshu.miaosha.domain.User;
import com.tongshu.miaosha.rabbitmq.MQSender;
import com.tongshu.miaosha.result.CodeMsg;
import com.tongshu.miaosha.result.Result;
import com.tongshu.miaosha.service.UserService;
import com.tongshu.miaosha.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;

@Controller
@RequestMapping("/demo")
public class DemoController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private MQSender mqSender;

    @RequestMapping("/hello")
    public String hello(Model model) {
        model.addAttribute("name", "tongshu");
        return "hello";
    }

    @RequestMapping("/myerror")
    public Result<String> error() {
        return Result.error(CodeMsg.SERVER_ERROR);
    }

    @RequestMapping("/db/get/{id}")
    @ResponseBody
    public Result<User> dbGet(@PathVariable("id") int id) {
        return Result.success(userService.getById(id));
    }

//    @RequestMapping("/mq")
//    @ResponseBody
//    public Result<String> mq() {
//        mqSender.send("hello, tongshu");
//        return Result.success("ok");
//    }
//
//    @RequestMapping("/mq/topic")
//    @ResponseBody
//    public Result<String> topic() {
//        mqSender.sendTopic("hello, tongshu");
//        return Result.success("ok");
//    }
//
//    @RequestMapping("/mq/fanout")
//    @ResponseBody
//    public Result<String> fanout() {
//        mqSender.sendFanout("hello, tongshu");
//        return Result.success("ok");
//    }
//
//    @RequestMapping("/mq/header")
//    @ResponseBody
//    public Result<String> header() {
//        mqSender.sendHeader("hello, tongshu");
//        return Result.success("ok");
//    }
}
